package com.fastloan.jn.mgt.customermanager.level.service.impl;

import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.customermanager.basic.dao.JnCustomerManagerMapper;
import com.fastloan.jn.mgt.customermanager.basic.model.JnCustomerManager;
import com.fastloan.jn.mgt.customermanager.basic.service.JnCustomerManagerService;
import com.fastloan.jn.mgt.customermanager.level.dao.*;
import com.fastloan.jn.mgt.customermanager.level.model.*;
import com.fastloan.jn.mgt.customermanager.level.service.*;
import com.fastloan.jn.utils.other.ObjUtil;
import com.fastloan.safe.core.util.ManagerKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author yanwe
 * createTime 2017-10-2017/10/17 17:26
 */
@Service
public class JnCustomerManagerLevelServiceImpl extends BasicServiceImpl<JnCustomerManagerLevel> implements JnCustomerManagerLevelService {

    @Autowired
    private JnCustomerManagerMapper jnCustomerManagerMapper;

    @Autowired
    private JnManagerLevelParamMapper managerLevelParamMapper ;

    @Autowired
    private JnManagerMonthAssessMapper managerMonthAssessMapper;

    @Autowired
    private JnManagerPerformanceParamMapper managerPerformanceParamMapper;

    @Autowired
    private JnManagerQuotaSettingMapper managerQuotaSettingMapper;

    @Autowired
    private JnManagerRiskMapper managerRiskMapper;



    @Override
    public List<JnCustomerManagerLevel> selectByOrg(String topOrgId){
        JnCustomerManagerLevel jnCustomerManagerLevel = new JnCustomerManagerLevel();
        jnCustomerManagerLevel.setOrganizationId(topOrgId);
        return select(jnCustomerManagerLevel);
    }

    @Override
    @Transactional
    public boolean addCustomerManagerLevel(JnCustomerManagerLevel jnCustomerManagerLevel){
        int result = insertSelective(jnCustomerManagerLevel);
        return result == 1;
    }

    @Override
    @Transactional
    public boolean updateCustomerManagerLevel(JnCustomerManagerLevel jnCustomerManagerLevel){
        int result = updateByPrimaryKeySelective(jnCustomerManagerLevel);
        return result == 1;
    }

    @Override
    @Transactional
    public boolean deleteCustomerManagerLevel(String levelId){
        JnCustomerManager jnCustomerManager = new JnCustomerManager();
        jnCustomerManager.setLevelId(levelId);
        int count = jnCustomerManagerMapper.selectCount(jnCustomerManager);
        if (count != 0){
            return false;
        }
        int result = deleteByPrimaryKey(levelId);
        return result == 1;
    }

    @Override
    @Transactional
    public void saveUpdate(JnCustomerManagerLevel managerLevel) {
        if(ObjUtil.isEmpty(managerLevel.getLevelId())){
            managerLevel.setCreateBy(ManagerKit.getUserId());
            managerLevel.setCreateTime(new Date());
            managerLevel.setOrganizationId(managerLevel.getOrganizationId());
            managerLevel.setIsDefaultLevel(0);
            insertSelective(managerLevel);
            managerLevelParamMapper.insertSelective(new JnManagerLevelParam().setLevelId(managerLevel.getLevelId()));
            managerMonthAssessMapper.insertSelective(new JnManagerMonthAssess().setLevelId(managerLevel.getLevelId()));
            managerPerformanceParamMapper.insertSelective(new JnManagerPerformanceParam().setLevelId(managerLevel.getLevelId()));
            managerRiskMapper.insertSelective(new JnManagerRisk().setLevelId(managerLevel.getLevelId()));
            managerQuotaSettingMapper.insertSelective(new JnManagerQuotaSetting().setLevelId(managerLevel.getLevelId()).setPeriod(1));
            managerQuotaSettingMapper.insertSelective(new JnManagerQuotaSetting().setLevelId(managerLevel.getLevelId()).setPeriod(2));
            managerQuotaSettingMapper.insertSelective(new JnManagerQuotaSetting().setLevelId(managerLevel.getLevelId()).setPeriod(3));
        }else{
            updateByPrimaryKeySelective(managerLevel);
        }
    }
}
